package org.example.SolutionNew002;

public class LinkedList04 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
//        head.next.next = new ListNode(2);
//        head.next.next.next = head.next;
        LinkedList04 l = new LinkedList04();
        System.out.println(l.hasCycle(head));
    }

    public boolean hasCycle(ListNode head) {
        if(head==null||head.next==null) return false;
        ListNode slow = head;
        ListNode fast = head;
        do {
            slow=slow.next;
            fast=fast.next.next;
            if(slow==fast) return true;
        }while(fast!=null && fast.next!=null && fast!=slow);
        return false;
    }
}
